package com.elong.businesstravel.modules.home;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.elong.businesstravel.R;
import com.elong.businesstravel.base.activity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.Poi;
import com.elong.businesstravel.modules.home.adapter.SimpleListAdapter;

/**
 * 选择地铁站
 * 
 * @author lenovo
 * 
 */
public class SelectSubwayActivity extends BaseActivity {

	private ListView mListView;
	private SimpleListAdapter mSimpleListAdapter;
	
	private List<Poi> mPois = new ArrayList<Poi>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_subway);

		init();
	}

	@Override
	protected void init() {
		
		mPois = (List<Poi>) getIntent().getSerializableExtra("Pois");
		
		List<String> strings = new ArrayList<String>();
		for (Poi poi : mPois) {
			String nameString = poi.name;
			if ("".equals(nameString)) {
				nameString = poi.bussinename;
			}
			if ("".equals(nameString)) {
				nameString = poi.districtsname;
			}
			strings.add(nameString);
		}
		
		mListView = (ListView)findViewById(R.id.listView);
		mSimpleListAdapter = new SimpleListAdapter(mContext, strings, "");
		mListView.setAdapter(mSimpleListAdapter);
		
		setListener();
		setTitleNavBar();
	}

	@Override
	protected void setListener() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.putExtra("Poi", mPois.get(arg2));
				setResult(RESULT_OK, intent);
				quit();
			}
		});
	}

	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("选择地铁站");
		titleNavBarView.setTitleBackground(R.color.bg_title_dialog);
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView
				.setLeftButtonOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						quit();
					}
				});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);

	}

	private void quit() {
		finish();
		overridePendingTransition(R.anim.activity_alpha_fixed,
				R.anim.activity_top_bottom_exit_anim_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
